package view;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import service.UserService;
import util.BaseFrame;
import util.MySpring;

public class LoginFrame extends BaseFrame {	
	public LoginFrame(String title) {
		super(title);
		this.init();
	}
	
	public LoginFrame() {
		this.init();
	}	
	
	private	JPanel mainPanel = new JPanel();
	private	JLabel titleLabel = new JLabel("驾 校 在 线 考 试 系 统");
	private	JLabel accountLabel = new JLabel("账 号：");
	private	JLabel passwordLabel = new JLabel("密 码：");
	private	JTextField accountField = new JTextField();
	private	JPasswordField passwordField = new JPasswordField();
	private	JButton loginButton = new JButton("登录");
	private	JButton registerButton = new JButton("注册");
	private	JButton exitButton = new JButton("退出");
	
	protected void setFontAndSoOn() {//子类修饰符范围大于等于父类		
		//设置panel的布局管理为自定义方式
		mainPanel.setLayout(null);
		titleLabel.setBounds(60, 40, 425, 35);
		titleLabel.setFont(new Font("微软雅黑",Font.BOLD,34));
		accountLabel.setBounds(94, 124, 90, 30);
		accountLabel.setFont(new Font("微软雅黑",Font.PLAIN,24));
		accountField.setBounds(204, 124, 260, 30);
		accountField.setFont(new Font("微软雅黑",Font.BOLD,24));
		passwordLabel.setBounds(94, 174, 90, 30);
		passwordLabel.setFont(new Font("微软雅黑",Font.PLAIN,24));
		passwordField.setBounds(204, 174, 260, 30);
		passwordField.setFont(new Font("",Font.BOLD,24));
		loginButton.setBounds(109,232,100,30);
		loginButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
		registerButton.setBounds(229,232,100,30);
		registerButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
		exitButton.setBounds(349,232,100,30);
		exitButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
	}
	
	protected void addElement() {
		mainPanel.add(titleLabel);
		mainPanel.add(accountLabel);
		mainPanel.add(accountField);
		mainPanel.add(passwordLabel);
		mainPanel.add(passwordField);
		mainPanel.add(loginButton);
		mainPanel.add(registerButton);
		mainPanel.add(exitButton);
		this.add(mainPanel);
	}
	
	protected void addListener() {
		ActionListener listener = new ActionListener(){//匿名内部类
			public void actionPerformed(ActionEvent e) {
				String account = accountField.getText();
				String password = passwordField.getText();//方法过时因为密码需要加密,方法可以使用(因为这里不对密码加密)
//				char[] value = passwordField.getPassword();//提前帮我们将密码拆解数组，让我们加密
//				String password = new String(value);
				UserService service = MySpring.getBean("service.UserService");
				String result = service.login(account, password);
				if(result.equals("登录成功")) {
					LoginFrame.this.setVisible(false);
					new ExamFrame(account+"的考试页面");
				}else {
					JOptionPane.showMessageDialog(LoginFrame.this, result);//因为这是匿名内部类所以直接使用this的话指向的不是Login类
					accountField.setText("");
					passwordField.setText("");
				}
			}
					
		};
		loginButton.addActionListener(listener);
		ActionListener RegisterListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				LoginFrame.this.setVisible(false);
				new RegisterFrame("注册页面");
			}
		};
		registerButton.addActionListener(RegisterListener);
		ActionListener ExitListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		};
		exitButton.addActionListener(ExitListener);
	}
	protected void setFrameSelf() {
		this.setCenterFrame(550, 360);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}
}
